package net.wesc.ac.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MainPanel extends VerticalPanel{
	private final ServiceAsync Service = GWT
	.create(Service.class);
	
	public final static String DH_EQUATION = "1";
	public final static String HE_EQUATION = "2";
	public final static String RS_EQUATION = "3";
	public final static String GS_EQUATION = "4";
	public final static String EN_EQUATION = "5";
	public final static String EU_EQUATION = "6";
	public final static String PP_EQUATION = "7";
	public final static String PE_EQUATION = "8";
	public final static String MB_EQUATION = "9";
	
	public final static String E11_TYPE = "11";
	public final static String E12_TYPE = "12";
	public final static String E13_TYPE = "13";
	public final static String E21_TYPE = "21";
	public final static String E22_TYPE = "22";
	public final static String E23_TYPE = "23";
	public final static String E31_TYPE = "31";
	public final static String E32_TYPE = "32";
	public final static String E33_TYPE = "33";
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	
	final static TextArea dataArea = new TextArea();
	final static TextBox tempBox = new TextBox();
	final static HTML resultHTML = new HTML();
	private Image image = new Image();
	public static double[] param = null;
	public static String model = MainPanel.DH_EQUATION; //default model
	public static String type = MainPanel.E11_TYPE; //default type
	
	final AlertDialog wait = new AlertDialog("Drawing,Please wait ......");

	public MainPanel(){
		//start menu
		MenuBar menu = new MenuBar(false);
		MenuBar plotMenu = new MenuBar(true);
		Command command = new Command(){

			@Override
			public void execute() {
				// TODO Auto-generated method stub
				ModelDialog model = new ModelDialog();
				model.show();
			}
			
		};
		
		Command expPlotCommand = new Command(){

			@Override
			public void execute() {
				// TODO Auto-generated method stub
				String data = dataArea.getValue();
				if(data.matches("")){
					Window.alert("Please Input Experimental Data First!");
					return;
				}
				else{
					wait.show();
					Service.expPlot(data, MainPanel.model, new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub
									Window.alert("RPC ERROR:" + caught.getMessage());
								}

								@Override
								public void onSuccess(String result) {
									// TODO Auto-generated method stub
									if(result!=null){
										image.setUrl(GWT.getHostPageBaseURL() + result);
										wait.hide();
									}
									else{
										Window.alert("Curve Plotting Error!");
										wait.hide();
									}
								}
						
					});
				}
			}
			
		};
		
		Command fitPlotCommand = new Command(){

			@Override
			public void execute() {
				// TODO Auto-generated method stub
				String data = dataArea.getValue();
				String temp = MainPanel.tempBox.getValue();
				if(data.matches("")){
					Window.alert("Please Input Experimental Data First!");
					wait.hide();
					return;
				}
				if(temp.matches("")){
					Window.alert("Please Input Temperature First!");
					wait.hide();
					return;
				}
				if(param == null){
					Window.alert("Please Fit Experimental Data First!");
					wait.hide();
					return;
				}
				if(Double.parseDouble(temp)<0 || Double.parseDouble(temp)>1000){
					Window.alert("Temperature is invalid!");
					wait.hide();
					return;
				}
				else{
					wait.show();
					Service.fitPlot(data, param, MainPanel.model, MainPanel.type, temp, new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub
									Window.alert("RPC ERROR:" + caught.getMessage());
								}

								@Override
								public void onSuccess(String result) {
									// TODO Auto-generated method stub
									if(result!=null){
										image.setUrl(GWT.getHostPageBaseURL() + result);
										wait.hide();
									}
									else{
										wait.hide();
										Window.alert("Curve Plotting Error!");
									}
								}
						
					});
				}
			}
			
		};
		
		Command bothPlotCommand = new Command(){

			@Override
			public void execute() {
				// TODO Auto-generated method stub
				String data = dataArea.getValue();
				String temp = MainPanel.tempBox.getValue();
				if(data.matches("")){
					Window.alert("Please Input Experimental Data First!");
					return;
				}
				if(temp.matches("")){
					Window.alert("Please Input Temperature First!");
					return;
				}
				if(param == null){
					Window.alert("Please Fit Experimental Data First!");
					return;
				}
				if(Double.parseDouble(temp)<0 || Double.parseDouble(temp)>1000){
					Window.alert("Temperature is invalid!");
					wait.hide();
					return;
				}
				else{
					wait.show();
					Service.bothPlot(data, param, MainPanel.model, MainPanel.type, temp, new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub
									Window.alert("RPC ERROR:" + caught.getMessage());
								}

								@Override
								public void onSuccess(String result) {
									// TODO Auto-generated method stub
									if(result!=null){
										image.setUrl(GWT.getHostPageBaseURL() + result);
										wait.hide();
									}
									else{
										Window.alert("Curve Plotting Error!");
									}
								}
						
					});
				}
			}
			
		};
		
		Command helpCommand = new Command(){

			@Override
			public void execute() {
				// TODO Auto-generated method stub
				AboutDialog dialog = new AboutDialog();
				dialog.show();
				dialog.setModal(true);
			}
			
		};
		
		MenuItem fitItem = new MenuItem("Fit",false,command);
		MenuItem plotItem = new MenuItem("Plot",false,(Command)null);
		MenuItem saveItem = new MenuItem("Save",false,command);
		MenuItem AboutItem = new MenuItem("About",false,helpCommand);
		MenuItem logoffItem = new MenuItem("Exit",false,command);
		MenuItem expPlotItem = new MenuItem("Exp Curve",false,expPlotCommand);
		MenuItem fitPlotItem = new MenuItem("Fit Curve",false,fitPlotCommand);
		MenuItem bothPlotItem = new MenuItem("Exp Curve&Fit Curve",false,bothPlotCommand);
		
		
		menu.setAnimationEnabled(true);
		
		plotMenu.addItem(expPlotItem);
		plotMenu.addSeparator();
		plotMenu.addItem(fitPlotItem);
		plotMenu.addSeparator();
		plotMenu.addItem(bothPlotItem);
		plotItem.setSubMenu(plotMenu);
		
		menu.addItem(fitItem);
		menu.addSeparator();
		menu.addItem(plotItem);
		menu.addSeparator();
		menu.addItem(saveItem);
		menu.addSeparator();
		menu.addItem(AboutItem);
		menu.addSeparator();
		menu.addItem(logoffItem);
		menu.addSeparator();
		
		this.add(menu);
		//end menu
		
		//start calcPanel
		HorizontalPanel calcPanel = new HorizontalPanel();
		calcPanel.setSize("1000px","620px");
		calcPanel.setBorderWidth(1);
		this.add(calcPanel);
		// end calcPanel
		
		VerticalPanel leftPanel = new VerticalPanel();
		leftPanel.setSize("300px", "620px");
		VerticalPanel rightPanel = new VerticalPanel();
		rightPanel.setSize("700px", "620px");
		
		calcPanel.add(leftPanel);
		calcPanel.add(rightPanel);
		
		//start left
		Label dataLabel = new Label("Experiment Data:");
		Label tempLabel = new Label("Experiment Temperature(0-1000K):");
		Label paramLabel = new Label("Optimized Parameters:");
		dataArea.setSize("300px", "350px");
		tempBox.setSize("300px", "20px");
		tempBox.setText("298.15");
		resultHTML.setSize("300px", "250px");
		leftPanel.add(dataLabel);
		leftPanel.add(dataArea);
		leftPanel.add(tempLabel);
		leftPanel.add(tempBox);
		leftPanel.add(paramLabel);
		leftPanel.add(resultHTML);
		//end left
		
		//start right
		image.setSize("600px", "520px");
		image.setAltText("Visualization Result");
		image.setUrl(GWT.getHostPageBaseURL() + "images/fig.jpg");
		rightPanel.setHorizontalAlignment(ALIGN_CENTER);
		rightPanel.setVerticalAlignment(ALIGN_MIDDLE);
		rightPanel.add(image);
		//end right
		
		wait.hide();
	}
}
